<?php

declare(strict_types=1);

namespace App\Job;

use App\Model\Message;
use App\Model\UserCoin;
use App\Model\UserProfile;
use Hyperf\AsyncQueue\Job;

class SysCallPopJob extends Job
{
    /**
     * 参数
     */
    public $params;

    /**
     * 任务执行失败后的重试次数，即最大执行次数为 $maxAttempts+1 次
     *
     * @var int
     */
    protected $maxAttempts = 1;

    public function __construct($params)
    {
        $this->params = $params;
    }

    public function handle()
    {
        $userId = $this->params['user_id'] ?? 0;
        if (!$userId) return;
        $this->doProccess($userId, $this->params['profile']);
        return;
    }

    function doProccess($userId = 0, $profile = null)
    {
        $userCoin = UserCoin::getUserInfo($userId);
        if ($userCoin['total_coin'] > 0) return;
        //发送透传消息
        $hostIds = UserProfile::getRecUsers(UserProfile::ROLE_HOST, 0);
        $pickHostId = $hostIds[array_rand($hostIds)];
        Message::sendSlient($userId, 'CALL_VIDEO_TO_PAY', ['user_id' => $pickHostId]);
        log_user_result($userId, $profile->nick_name);
    }
}
